package com.zhiwang.planet.ui.my

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.PhoneBindEvent
import com.zhiwang.planet.event.TabChangeEvent
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.UserInfoView
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.mvvm.viewmodel.ShareViewModel
import com.zhiwang.planet.mvvm.viewmodel.main.sharedViewModels
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.MainActivity
import com.zhiwang.planet.ui.StatusbarUtil
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.my.record.DiamondFragment
import com.zhiwang.planet.ui.my.record.EnergyFragment
import com.zhiwang.planet.util.Store
import com.zhiwang.planet.util.quickStartActivity
import com.zhiwang.planet.util.suicideAfterTransition
import com.zhiwang.planet.widget.SimpleDialogBuilder
import kotlinx.android.synthetic.main.activity_my_detail.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import javax.inject.Inject

class MyDetailActivity : BaseActivity(), UserInfoView {

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        userPresenter.userInfoView = this
    }

    @Inject
    lateinit var userPresenter: UserPresenter

    val sharedViewModel by sharedViewModels<ShareViewModel>()

    var phoneBind = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_detail)
        StatusbarUtil.setStatusBarColor(this, R.color.colorPlanetBg)
        flycoTabLayout.setDefaultTabData(arrayListOf(MainActivity.Tab.of("趣钻明细", 0, 0), MainActivity.Tab.of("趣豆明细", 0, 0)),
                supportFragmentManager,
                R.id.container, arrayListOf(DiamondFragment(), EnergyFragment()
        ))

        detail_about.setOnClickListener {
            WebViewActivity.load(this, Store.URL_TIPS)
        }

        navBack.setOnClickListener { onBackPressed() }


        sharedViewModel.diamondCount.observe(this, Observer {

            diamondNumber.text = it

        })

        sharedViewModel.beanCount.observe(this, Observer {
            energyNumber.text = it

        })
    }

    override fun onResume() {
        super.onResume()
        userPresenter.queryUserInfo()
    }

    override fun onUserInfoLoad(user: User?) {
        phoneBind = TextUtils.isEmpty(user?.phone).not()
    }

    fun withdraw(v: View) {
        if (phoneBind)
            quickStartActivity(WithdrawActivity::class.java)
        else showBindPhoneAlertDialog()
    }

    private fun showBindPhoneAlertDialog() {
        SimpleDialogBuilder()
                .title("提示")
                .titleTextColor(R.color.colorPrimary)
                .buttonText("我知道了")
                .content("请先绑定手机号")
                .onButtonClick(View.OnClickListener {
                    quickStartActivity(BindPhoneActivity::class.java)
                })
                .build(this)
                .show()
    }

    fun getPower(v: View) {
        EventBus.getDefault().post(TabChangeEvent(3))
        suicideAfterTransition()

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPhoneBind(e: PhoneBindEvent){
        userPresenter.queryUserInfo()
    }

}
